package zhxt.helmet.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import zhxt.helmet.dao.provider.ResultSqlProvider;
import zhxt.helmet.pojo.Result;

import java.sql.Timestamp;
import java.util.List;

@Repository
public interface ResultDao {
    // 根据多个摄像头 id 和时间范围选择结果
    @SelectProvider(type = ResultSqlProvider.class, method = "buildSelectResultsByCameraIdsSql")
    @Results({
            @org.apache.ibatis.annotations.Result(column = "camera_id", property = "cameraId"),
            @org.apache.ibatis.annotations.Result(column = "detect_time", property = "detectTime"),
            @org.apache.ibatis.annotations.Result(column = "have_helmet", property = "haveHelmet"),
    })
    List<Result> selectResults(@Param("cameraIdList") List<Integer> cameraIdList,
                                           @Param("startTime") Timestamp startTime,
                                           @Param("endTime") Timestamp endTime);
    //插入最新识别结果
    @Insert("INSERT INTO result(camera_id, detect_time, have_helmet) " +
            "VALUES(#{cameraId}, #{detectTime}, #{haveHelmet})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertResult(Result result);
    //批量插入
    @Insert({
            "<script>",
            "INSERT INTO result (camera_id, detect_time, have_helmet) VALUES ",
            "<foreach collection='list' item='item' separator=','>",
            "(#{item.cameraId}, #{item.detectTime}, #{item.haveHelmet})",
            "</foreach>",
            "</script>"
    })
    void insertResults(List<Result> results);
}
